package mobi.springtour;

import java.util.ArrayList;
import java.util.Calendar;


import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class LoginActivity extends Activity {
	private Button login, submit, dateButton, timeButton, getJsonData;
	private EditText username;
	private EditText password;
	private Spinner operator;
	private RadioGroup radioGroup;
	private TextView whoIsMostBeautiful, whatDoYouLike, dateMessage;
	private DatePicker datePicker;
	private TimePicker timePicker;
	private Calendar c;
	private RadioButton button1, button2, button3;
	private CheckBox checkBox1, checkBox2, checkBox3;
	private final static String USERNAME = "admin";
	private final static String PASSWORD = "1";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		try {
			// 全屏
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
			setContentView(R.layout.main);
			username = (EditText) findViewById(R.id.username);
			password = (EditText) findViewById(R.id.password);
			whenLoginOnClick();
			whenExitOnClick();
			setSpinner();
			setRadioGroup();
			setCheckBox();
			setPicker();
			setGetJsonData();
		} catch (Exception e) {
			Log.v("LoginActivity", e.getMessage(), e);
		}
	}

	public void setGetJsonData() {
		getJsonData = (Button) findViewById(R.id.showJsonDateButton);
		getJsonData.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent netIntent = new Intent(LoginActivity.this,
						NetActivity.class);
				startActivity(netIntent);
			}
		});
	}

	private void setPicker() {
		dateMessage = (TextView) findViewById(R.id.dateMessage);
		dateButton = (Button) findViewById(R.id.dateButton);
		timeButton = (Button) findViewById(R.id.timeButton);
		datePicker = (DatePicker) findViewById(R.id.DatePicker);
		timePicker = (TimePicker) findViewById(R.id.TimePicker);
		c = Calendar.getInstance();
		datePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
				c.get(Calendar.DAY_OF_MONTH),
				new DatePicker.OnDateChangedListener() {
					@Override
					public void onDateChanged(DatePicker paramDatePicker,
							int year, int month, int day) {
						c.set(year, month, day);
						Toast.makeText(LoginActivity.this,
								"日期改变了:" + year + "-" + month + "-" + day,
								Toast.LENGTH_LONG).show();
					}
				});

		timePicker.setIs24HourView(true);

		timePicker
				.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
					@Override
					public void onTimeChanged(TimePicker paramTimePicker,
							int paramInt1, int paramInt2) {
						// c.set(year, month, day, hourOfDay, minute)
					}
				});

		dateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View paramView) {
				new DatePickerDialog(LoginActivity.this,
						new DatePickerDialog.OnDateSetListener() {
							@Override
							public void onDateSet(DatePicker paramDatePicker,
									int paramInt1, int paramInt2, int paramInt3) {
								// 设置日历

							}
						}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c
								.get(Calendar.DAY_OF_MONTH)).show();
			}
		});

		timeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View paramView) {
				new TimePickerDialog(LoginActivity.this,
						new TimePickerDialog.OnTimeSetListener() {

							@Override
							public void onTimeSet(TimePicker paramTimePicker,
									int paramInt1, int paramInt2) {
								// 设置时间
							}
						}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE),
						true).show();
			}
		});
	}

	private void setCheckBox() {
		submit = (Button) findViewById(R.id.submit);
		whatDoYouLike = (TextView) findViewById(R.id.TextView02);
		checkBox1 = (CheckBox) findViewById(R.id.CheckBox01);
		checkBox2 = (CheckBox) findViewById(R.id.CheckBox02);
		checkBox3 = (CheckBox) findViewById(R.id.CheckBox03);
		checkBox1
				.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(
							CompoundButton paramCompoundButton,
							boolean paramBoolean) {
						if (paramBoolean) {
							Toast.makeText(LoginActivity.this,
									"你选择了:" + checkBox1.getText(),
									Toast.LENGTH_LONG).show();
						} else {
							Toast.makeText(LoginActivity.this,
									"你反选了:" + checkBox1.getText(),
									Toast.LENGTH_LONG).show();
						}

					}

				});

		checkBox2
				.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(
							CompoundButton paramCompoundButton,
							boolean paramBoolean) {
						if (paramBoolean) {
							Toast.makeText(LoginActivity.this,
									"你选择了:" + checkBox2.getText(),
									Toast.LENGTH_LONG).show();
						} else {
							Toast.makeText(LoginActivity.this,
									"你反选了:" + checkBox2.getText(),
									Toast.LENGTH_LONG).show();
						}

					}

				});

		checkBox3
				.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(
							CompoundButton paramCompoundButton,
							boolean paramBoolean) {
						if (paramBoolean) {
							Toast.makeText(LoginActivity.this,
									"你选择了:" + checkBox3.getText(),
									Toast.LENGTH_LONG).show();
						} else {
							Toast.makeText(LoginActivity.this,
									"你反选了:" + checkBox3.getText(),
									Toast.LENGTH_LONG).show();
						}

					}

				});
		submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				int num = 0;
				StringBuffer text = new StringBuffer();
				if (checkBox1.isChecked()) {
					num++;
					text.append(checkBox1.getText() + ",");
				}
				if (checkBox2.isChecked()) {
					num++;
					text.append(checkBox2.getText() + ",");
				}
				if (checkBox3.isChecked()) {
					num++;
					text.append(checkBox3.getText());
				}
				Toast.makeText(LoginActivity.this,
						"你一共选择了:" + num + "个/" + text.toString(),
						Toast.LENGTH_LONG).show();
			}
		});
	}

	private void setRadioGroup() {
		whoIsMostBeautiful = (TextView) findViewById(R.id.TextView01);
		radioGroup = (RadioGroup) findViewById(R.id.RadioGroup01);
		button1 = (RadioButton) findViewById(R.id.RadioButton1);
		button2 = (RadioButton) findViewById(R.id.RadioButton2);
		button3 = (RadioButton) findViewById(R.id.RadioButton3);
		radioGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						if (checkedId == button1.getId()) {
							Toast.makeText(LoginActivity.this, "有同事喜欢这款！",
									Toast.LENGTH_LONG).show();
						} else {
							Toast.makeText(LoginActivity.this, "没有同事喜欢这款！",
									Toast.LENGTH_LONG).show();
						}
					}
				});

	}

	private void setSpinner() {
		operator = (Spinner) findViewById(R.id.operatorSp);
		ArrayList<String> list = new ArrayList<String>();
		// 获取相应对象
		String[] ls = getResources().getStringArray(R.array.operators);
		// 获取XML中定义的数组
		for (int i = 0; i < ls.length; i++) {
			list.add(ls[i]);
		}
		// 把数组导入到ArrayList中
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 设置下拉菜单的风格
		operator.setAdapter(adapter);

	}

	/**
	 * 登录事件
	 */
	private void whenLoginOnClick() {
		login = (Button) findViewById(R.id.login);
		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					final String userName = username.getText().toString();
					final String passWord = password.getText().toString();
					if (LoginActivity.USERNAME.equals(userName)
							&& LoginActivity.PASSWORD.equals(passWord)) {
						Toast.makeText(LoginActivity.this, "成功登录！",
								Toast.LENGTH_LONG).show();
						Intent ticketIntent = new Intent(LoginActivity.this,
								TicketActivity.class);
						Bundle bundle = new Bundle();
						bundle.putString("USERNAME", "春秋国旅管理员");
						ticketIntent.putExtras(bundle);
						startActivity(ticketIntent);
					} else {
						Toast.makeText(LoginActivity.this, "登录失败！",
								Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					// 暂时打到控制台
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * 退出事件
	 */
	private void whenExitOnClick() {
		// exit = (Button) findViewById(R.id.exit);
		// exit.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View paramView) {
		// Toast.makeText(LoginActivity.this, "退出系统!", Toast.LENGTH_LONG)
		// .show();
		// finish();
		//
		// }
		// });
	}
}
